package wn.tcpip.socket.netty.business;

import com.alibaba.fastjson.JSON;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import wn.tcpip.socket.netty.business.factory.ResoverAbs;
import wn.tcpip.socket.netty.cache.NettyChannelMap;
import wn.tcpip.socket.netty.model.GpsStateEntity;
import wn.tcpip.socket.util.ProtocolUtil;
import wn.tcpip.socket.netty.model.GPSBackRequest;
import wn.tcpip.socket.netty.model.GpsEquipmentPositionEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;

/**
 * Created by zhangqin on 2017/4/19.
 */
@Service
public class GpsInfoAndStateResoverImp extends ResoverAbs {
    private static  final Logger logger = LoggerFactory.getLogger(GpsInfoAndStateResoverImp.class);
    @Resource
    private Queue gpsInfoQueue;
    @Resource
    private Queue stateInfoQueue;
    @Resource
    private JmsTemplate jmsQueueTemplate;
    @Override
    public String getResponse(GPSBackRequest request) {

        String imieByChannel = NettyChannelMap.get(request.getChannel());
        if(StringUtils.isBlank(imieByChannel)){
            logger.error("没有登录--------");
            return null;
        }else{
            GpsEquipmentPositionEntity entity = ProtocolUtil.parseEntity(request.getContent());
            entity.setImei(imieByChannel);
            jmsQueueTemplate.send(gpsInfoQueue, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage(JSON.toJSONString(entity));
                }
            });
            jmsQueueTemplate.send(stateInfoQueue, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage(JSON.toJSONString(new GpsStateEntity(imieByChannel, entity.getState())));
                }
            });
            return super.makeDefaultResponse(request);
        }

    }
}
